package com.niannian.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

public abstract class LoadDataUtil {
	public static int longtimeout = 9000;
	public static int smalltimeout = 3000;
	private LoadDataAsync serverLoad = null;
	Map<String, Object> parameters = null;
	private int timeout = 3000;
	private boolean isrefresh = true;
	private Context context;

	public void loadOnlyDb() {
		loadOnlyDb(false, null);
	}

	public void loadOnlyDb(boolean refresh, Map<String, Object> param) {
		if (serverLoad == null) {
			serverLoad = new LoadDataAsync();
		}
		if (param != null) {
			parameters = param;
		}
		if (parameters == null) {
			parameters = new HashMap<String, Object>();
		}
		isrefresh = refresh;
		loadMoreDB(parameters, isrefresh);
	}

	public void load() {
		load(null);
	}

	public void load(Map<String, Object> param) {
		if (serverLoad == null) {
			serverLoad = new LoadDataAsync();
		}
		if (param != null) {
			parameters = param;
		}
		if (parameters == null) {
			parameters = new HashMap<String, Object>();
		}
		isrefresh = false;
		serverLoad.setParams(parameters);
		serverLoad.setContext(context);
		serverLoad.execute();
	}

	public void refresh() {
		refresh(null);
	}

	public void refresh(Map<String, Object> param) {
		timeout = smalltimeout;
		if (serverLoad == null) {
			serverLoad = new LoadDataAsync();
		}
		if (param != null) {
			parameters = param;
		} else {
			parameters = new HashMap<String, Object>();
		}
		isrefresh = true;
		serverLoad.setParams(parameters);
		serverLoad.setContext(context);
		serverLoad.execute();
	}

	protected abstract String runServerTask(Map<String, Object> parameters);

	protected void serverTaskFinally(Map<String, Object> params) {
	}

	protected abstract boolean handleServerSuccess(JSONObject json, Map<String, Object> parameters);

	protected abstract boolean loadMoreDB(Map<String, Object> parameters, boolean refresh);

	class LoadDataAsync extends AsyncHandle {
		protected void netWorkFail(Map<String, Object> parameters) {
			if (!isrefresh) {
				if (!loadMoreDB(parameters, isrefresh)) {
					timeout = longtimeout;
				}
			}
		}

		protected void errorFinally(Map<String, Object> params) {
			serverTaskFinally(params);
		}

		protected void handleData(JSONObject json, Map<String, Object> parameters) throws JSONException {
			boolean notload = handleServerSuccess(json, parameters);
			if (!notload) {
				if (!loadMoreDB(parameters, isrefresh)) {
					timeout = longtimeout;
				}
			}
		}

		protected String runTask(Map<String, Object> parameters) {
			return runServerTask(parameters);
		}
	}

	public Map<String, Object> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, Object> parameters) {
		this.parameters = parameters;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public void setContext(Context cont) {
		this.context = cont;
	};
}
